package leetcode.code0155;

import java.util.Stack;

public class MinStack extends Solution155 {

	private Stack<Long> s;
	private int min;

	public MinStack() {
		s = new Stack<>();
	}

	public void push(int val) {
		if (s.isEmpty()) {
			s.add((long)val);
			min = val;
		} else {
			s.add((long)val - min);
			if (val < min) {
				min = val;
			}
		}
	}

	public void pop() {
		long diff = s.pop();
		if (s.size() == 0) {
			min = 0;
		}
		if (diff < 0) {
			min = (int) (min - diff);
		}
	}

	public int top() {
		return s.size() == 1 ? min : s.peek() < 0 ? min : (int)(min + s.peek());
	}

	public int getMin() {
		return min;
	}

	public static void main(String[] args) {
		int s = 0;
		MinStack minStack = new MinStack();
//		minStack.push(2147483646);
//		minStack.push(2147483646);
//		minStack.push(2147483646);
//		s = minStack.top();
//		System.out.println(s);
//		minStack.pop();
//		s = minStack.getMin();
//		System.out.println(s);
//		minStack.pop();
//		s = minStack.getMin();
//		System.out.println(s);
//		minStack.pop();
		minStack.push(2147483646);
		s = minStack.top();
		System.out.println(s);
//		s = minStack.getMin();
//		System.out.println(s);
		minStack.push(-2147483648);
//		s = minStack.top();
//		System.out.println(s);
//		s = minStack.getMin();
//		System.out.println(s);
		minStack.pop();
		s = minStack.getMin();
		System.out.println(s);
//		System.out.println(-2147483648);

	}

}
